//
//  RBCompeLiveListCell.m
//  RMMaterialRed
//
//  Created by XBWH on 2024/3/13.
//

#import "RBCompeLiveListCell.h"
#import "RBCompeLiveTextModel.h"
#import "RBCompeBasBallModel.h"

@interface RBCompeLiveListCell ()
@property(strong,nonatomic)UIView *bgView;
@property(strong,nonatomic)UIView *leftView;
@property(strong,nonatomic)UILabel *leftLabel;
@property(strong,nonatomic)UILabel *timeLabel;
@property(strong,nonatomic)UIView *rightView;
@property(strong,nonatomic)UILabel *rightLabel;
@property(copy,nonatomic)NSString *currentStr;
@end

@implementation RBCompeLiveListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self rb_initViewUI];
        [self setNeedsLayout];
    }
    return self;
}
-(void)rb_initViewUI{
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.leftView];
    [self.leftView addSubview:self.leftLabel];
    [self.bgView addSubview:self.topLineView];
    [self.bgView addSubview:self.timeLabel];
    [self.bgView addSubview:self.bottomLineView];
    [self.bgView addSubview:self.rightView];
    [self.rightView addSubview:self.rightLabel];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.mas_equalTo(0);
    }];
    [self.leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(RBScaleWidth(12));
        make.top.mas_equalTo(RBScaleWidth(10));
        make.width.mas_equalTo(RBScaleWidth(145));
        make.bottom.mas_equalTo(0);
    }];
    self.leftView.layer.cornerRadius = 2;
    self.leftView.layer.masksToBounds = YES;
    [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(RBScaleWidth(10));
        make.left.mas_equalTo(RBScaleWidth(10));
        make.right.mas_equalTo(-RBScaleWidth(10));
        make.bottom.mas_equalTo(-RBScaleWidth(10));
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(RBScaleWidth(10));
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(RBScaleWidth(28));
        make.width.mas_equalTo(RBScaleWidth(50));
    }];
    self.timeLabel.layer.cornerRadius = RBScaleWidth(14);
    self.timeLabel.layer.masksToBounds = YES;
    self.timeLabel.layer.borderColor = RBColor(204, 204, 204).CGColor;
    self.timeLabel.layer.borderWidth = 0.5;
    [self.topLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.centerX.mas_equalTo(self.timeLabel.mas_centerX);
        make.width.mas_equalTo(0.5);
        make.bottom.mas_equalTo(self.timeLabel.mas_top);
    }];
    [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.timeLabel.mas_centerX);
        make.width.mas_equalTo(0.5);
        make.top.mas_equalTo(self.timeLabel.mas_bottom);
        make.bottom.mas_equalTo(0);
    }];
    [self.rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-RBScaleWidth(12));
        make.top.mas_equalTo(RBScaleWidth(10));
        make.width.mas_equalTo(RBScaleWidth(145));
        make.bottom.mas_equalTo(0);
    }];
    self.rightView.layer.cornerRadius = 2;
    self.rightView.layer.masksToBounds = YES;
    [self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(RBScaleWidth(10));
        make.left.mas_equalTo(RBScaleWidth(10));
        make.right.mas_equalTo(-RBScaleWidth(10));
        make.bottom.mas_equalTo(-RBScaleWidth(10));
    }];
}
-(void)setModel:(RBCompeLiveTextModel *)model{
    _model=model;
    self.leftLabel.text = model.data;
    self.rightLabel.text = model.data;
    NSMutableAttributedString *hintString =[[NSMutableAttributedString alloc]initWithString: self.leftLabel.text];
    //获取要调整颜色的文字位置
    if ([self.leftLabel.text containsString:_awayName]) {
        NSRange range2 =[[hintString string]rangeOfString:_awayName];
        [hintString addAttribute:NSForegroundColorAttributeName value:RBColor(76, 136, 255) range:range2];
        self.leftView.hidden = NO;
        self.rightView.hidden = YES;
        self.leftLabel.attributedText = hintString;
    }else{
        self.leftView.hidden = YES;
        self.rightView.hidden = NO;
    }
    
    if ([self.rightLabel.text containsString:_homeName]) {
        NSRange range =[[hintString string]rangeOfString:_homeName];
        [hintString addAttribute:NSForegroundColorAttributeName value:RBColor(242, 48, 48) range:range];
        self.leftView.hidden = YES;
        self.rightView.hidden = NO;
        self.rightLabel.attributedText = hintString;
    }else{
        self.leftView.hidden = NO;
        self.rightView.hidden = YES;
    }
    
    //设置背景色
    if ([self.leftLabel.text containsString:_awayName]) {
        self.leftView.backgroundColor = RBColor(235, 241, 255);
        self.timeLabel.backgroundColor = RBColor(235, 241, 255);
    }else{
        self.leftView.backgroundColor = RBColor(245, 245, 245);
        if ([self.rightLabel.text containsString:_homeName]){
            self.timeLabel.backgroundColor = RBColor(255, 235, 235);
        }else{
            self.timeLabel.backgroundColor = RBColor(255, 255, 255);
        }
    }
    
    NSString *titleStr;
    CGFloat timeW;
    if (![RBTool rb_isEmptyStr:model.time]) {
        titleStr = model.time;
        timeW = RBScaleWidth(28);
    }else{
        timeW = RBScaleWidth(40);
        if ([model.data containsString:@"开始"]) {
            titleStr = @"开始";
        } else if([model.data containsString:@"天气"]){
            titleStr = @"天气";
        } else if([model.data containsString:@"场地"]){
            titleStr = @"场地";
        }else if([model.data containsString:@"结束"]){
            titleStr = @"结束";
        }else{
            titleStr = @"准备";
        }
    }
    [self.timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(timeW);
    }];
    self.timeLabel.text = titleStr;

    [self setNeedsLayout];
}
-(void)setBasModel:(RBCompeBasBallModel *)basModel{
    _basModel=basModel;
    NSString *modelStr = basModel.midestrTilte;
    NSString *jieStr;
    if ([modelStr containsString:@"$第0$"]) {
        jieStr = @"第一节";
    }else if ([modelStr containsString:@"$第1$"]){
        jieStr = @"第二节";
    }else if ([modelStr containsString:@"$第2$"]){
        jieStr = @"第三节";
    }else if ([modelStr containsString:@"$第3$"]){
        jieStr = @"第四节";
    }else{
        jieStr = @"加时赛";
    }
    modelStr = [modelStr stringByReplacingOccurrencesOfString:@"$第0$" withString:@""];
    modelStr = [modelStr stringByReplacingOccurrencesOfString:@"$第1$" withString:@""];
    modelStr = [modelStr stringByReplacingOccurrencesOfString:@"$第2$" withString:@""];
    modelStr = [modelStr stringByReplacingOccurrencesOfString:@"$第3$" withString:@""];
    NSMutableArray *labArry = [modelStr componentsSeparatedByString:@"^"].mutableCopy;
    [labArry removeObjectsInRange:NSMakeRange(6, 5)];
    //设置左边数据
    self.leftLabel.text = jieStr;
    self.leftLabel.text = [NSString stringWithFormat:@"%@ %@",self.leftLabel.text,labArry[1]];
    self.leftLabel.text = [NSString stringWithFormat:@"%@ [%@] ",self.leftLabel.text,labArry[4]];
    NSString *teamStr = labArry[2];
    if ([teamStr intValue] == 1) {
        self.leftLabel.text = [NSString stringWithFormat:@"%@ %@",self.leftLabel.text,_homeName];
    }else if ([teamStr intValue] == 2){
        self.leftLabel.text = [NSString stringWithFormat:@"%@ %@",self.leftLabel.text,_awayName];
    }
    self.leftLabel.text = [NSString stringWithFormat:@"%@%@",self.leftLabel.text,labArry[5]];
    //设置右边数据
    self.rightLabel.text = jieStr;
    self.rightLabel.text = [NSString stringWithFormat:@"%@ %@",self.rightLabel.text,labArry[1]];
    self.rightLabel.text = [NSString stringWithFormat:@"%@ [%@] ",self.rightLabel.text,labArry[4]];
    if ([teamStr intValue] == 1) {
        self.rightLabel.text = [NSString stringWithFormat:@"%@ %@",self.rightLabel.text,_homeName];
    }else if ([teamStr intValue] == 2){
        self.rightLabel.text = [NSString stringWithFormat:@"%@ %@",self.rightLabel.text,_awayName];
    }
    self.rightLabel.text = [NSString stringWithFormat:@"%@%@",self.rightLabel.text,labArry[5]];
    NSMutableAttributedString *hintString =[[NSMutableAttributedString alloc]initWithString: self.leftLabel.text];
    //获取要调整颜色的文字位置
    if ([self.leftLabel.text containsString:_awayName]) {//客队
        NSRange range2 =[[hintString string]rangeOfString:_awayName];
        [hintString addAttribute:NSForegroundColorAttributeName value:RBColor(76, 136, 255) range:range2];
        [self rb_addAttribute:hintString];
        self.leftView.hidden = NO;
        self.rightView.hidden = YES;
        self.currentStr = self.leftLabel.text;
        self.leftLabel.attributedText = hintString;
    }else{
        self.leftView.hidden = YES;
        self.rightView.hidden = NO;
        self.currentStr = self.rightLabel.text;
    }
    if ([self.rightLabel.text containsString:_homeName]) {//主队
        NSRange range =[[hintString string]rangeOfString:_homeName];
        [hintString addAttribute:NSForegroundColorAttributeName value:RBColor(242, 48, 48) range:range];
        [self rb_addAttribute:hintString];
        self.leftView.hidden = YES;
        self.rightView.hidden = NO;
        self.currentStr = self.rightLabel.text;
        self.rightLabel.attributedText = hintString;
    }else{
        self.leftView.hidden = NO;
        self.rightView.hidden = YES;
        self.currentStr = self.leftLabel.text;
    }
    
    if ([self.leftLabel.text containsString:_awayName]) {
        self.leftView.backgroundColor = RBColor(235, 241, 255);
        self.timeLabel.backgroundColor = RBColor(235, 241, 255);
    }else{
        self.leftView.backgroundColor = RBColor(245, 245, 245);
        if ([self.rightLabel.text containsString:_homeName]){
            self.timeLabel.backgroundColor = RBColor(255, 235, 235);
        }else{
            self.timeLabel.backgroundColor = RBColor(255, 255, 255);
        }
    }
    
    NSString *timeStr = [labArry rb_objectAtIndex:1];
    self.timeLabel.text = jieStr;
    
    CGFloat cellH = [self.currentStr rb_sizeWithFont:RBPingFangFont(12) constrainedToWidth:(RBScaleWidth(145)-RBScaleWidth(20))].height+RBScaleWidth(20)+RBScaleWidth(10);
    basModel.basCellHeight = cellH;
    
    [self setNeedsLayout];
}
//设置富文本
-(void)rb_addAttribute:(NSMutableAttributedString *)hintString{
    NSRange range4 = [[hintString string]rangeOfString:@"第一节"];
    [hintString addAttribute:NSForegroundColorAttributeName value:RBColor(51, 51, 51) range:range4];
    NSRange range5 = [[hintString string]rangeOfString:@"第二节"];
    [hintString addAttribute:NSForegroundColorAttributeName value:RBColor(51, 51, 51) range:range5];
    NSRange range6 = [[hintString string]rangeOfString:@"第三节"];
    [hintString addAttribute:NSForegroundColorAttributeName value:RBColor(51, 51, 51) range:range6];
    NSRange range7 = [[hintString string]rangeOfString:@"第四节"];
    [hintString addAttribute:NSForegroundColorAttributeName value:RBColor(51, 51, 51) range:range7];
}
- (UIView *)bgView{
    if(_bgView==nil){
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}
- (UIView *)leftView{
    if(_leftView==nil){
        _leftView = [[UIView alloc] init];
        _leftView.backgroundColor = RBColor(245, 245, 245);
    }
    return _leftView;
}
-(UILabel *)leftLabel{
    if(_leftLabel==nil){
        _leftLabel = [UILabel rb_labelWithTitle:@"大家好，欢迎收看本场 比赛直播，球员们正在 热身，比赛即将开始！" color:RBColor(102, 102, 102) font:RBPingFangFont(12) alignment:NSTextAlignmentLeft];
        _leftLabel.numberOfLines = 0;
    }
    return _leftLabel;
}
- (UIView *)topLineView{
    if(_topLineView==nil){
        _topLineView = [[UIView alloc] init];
        _topLineView.backgroundColor = RBColor(204, 204, 204);
    }
    return _topLineView;
}
-(UILabel *)timeLabel{
    if(_timeLabel==nil){
        _timeLabel = [UILabel rb_labelWithTitle:@"准备" color:RBColor(51, 51, 51) font:RBPingFangMediumFont(11) alignment:NSTextAlignmentCenter];
        _timeLabel.backgroundColor = RBColor(255, 235, 235);
    }
    return _timeLabel;
}
- (UIView *)bottomLineView{
    if(_bottomLineView==nil){
        _bottomLineView = [[UIView alloc] init];
        _bottomLineView.backgroundColor = RBColor(204, 204, 204);
    }
    return _bottomLineView;
}
- (UIView *)rightView{
    if(_rightView==nil){
        _rightView = [[UIView alloc] init];
        _rightView.backgroundColor = RBColor(255, 235, 235);
    }
    return _rightView;
}
-(UILabel *)rightLabel{
    if(_rightLabel==nil){
        _rightLabel = [UILabel rb_labelWithTitle:@"大家好，欢迎收看本场 比赛直播，球员们正在 热身，比赛即将开始！" color:RBColor(102, 102, 102) font:RBPingFangFont(12) alignment:NSTextAlignmentLeft];
        _rightLabel.numberOfLines = 0;
        
    }
    return _rightLabel;
}

@end
